<?php

/**
 * Description of PDO_Mysql herite de la classe PDO
 *
 * @author Fabrice Antoine
 */
class PDO_Mysql extends PDO{

    private $connexion,$engine, $host, $database, $user, $pass;

    /**
     * Le constructeur de la classe
     * Surcharge de la classe PDO
     * @param <string> $host
     * @param <string> $dbname
     * @param <string> $login
     * @param <string> $pwd
     */
    function __construct() {
        $this->engine = 'mysql';
        $this->host = 'localhost';
        $this->database = 'friendsplus';
        $this->user = 'root';
        $this->pass = '';

        $dsn = $this->engine.':host ='.$this->host.' ;dbname='.$this->database;

        // 1er méthode en instanciant par l'objet PDO
//        if (!$this->connexion = new PDO($dsn, $this->user, $this->pass))
//            throw new Exception('Erreur de connexion PDO');

        // 2ème méthode en heritant de la classe PDO
        parent::__construct($dsn, $this->user, $this->pass);
    }

    /**
     * Exécute une Requete
     * @param <string> $query
     * @return <object>
     */
    public function pquery($query) {
        try {
            $result = parent::query($query);
            
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            echo '<b>Erreur Querry .</b>'.$exc->getMessage();
        }
        return $result;
    }

    public function pfetch ($result) {
        return $result->fetch(PDO::FETCH_ASSOC);
    }

    public function pfetchAll ($result) {
        return $result->fetchAll(PDO::FETCH_ASSOC);
    }
    
    public function pfetchObj ($result) {
        return $result->fetch(PDO::FETCH_OBJ);
    }

    /**
     *
     * @param <string> $query
     * @param <array> $data
     * @param <string> $primary_id | permet de retourner la valeur de l'id 
     * @return <array> 
     */
    public function pinsert($query, $data, $primary_id = NULL){
        $q = parent::prepare($query);
        if (!$q)
            throw new Exception('Erreur prepare query');

        $row = $q->execute($data);
        if ($primary_id)
            $row = parent::lastInsertId($primary_id);

        if (!$row)
            throw new Exception('Erreur insert execute Email déjà existant');
        
        return $row;
    }

    public function  __destruct() {
    }

}

?>